package com.nanhai.buddhist.college.module.study.service.studyevaluationitem;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.nanhai.buddhist.college.module.study.controller.admin.studyevaluationitem.vo.*;
import com.nanhai.buddhist.college.module.study.dal.dataobject.studyevaluationitem.StudyEvaluationItemDO;
import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;

import com.nanhai.buddhist.college.module.study.convert.studyevaluationitem.StudyEvaluationItemConvert;
import com.nanhai.buddhist.college.module.study.dal.mysql.studyevaluationitem.StudyEvaluationItemMapper;

import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.nanhai.buddhist.college.module.study.enums.ErrorCodeConstants.*;

/**
 * 评价项目 Service 实现类
 *
 * @author 南海佛学院
 */
@Service
@Validated
public class StudyEvaluationItemServiceImpl implements StudyEvaluationItemService {

    @Resource
    private StudyEvaluationItemMapper evaluationItemMapper;

    @Override
    public String createEvaluationItem(StudyEvaluationItemCreateReqVO createReqVO) {
        // 插入
        StudyEvaluationItemDO evaluationItem = StudyEvaluationItemConvert.INSTANCE.convert(createReqVO);
        evaluationItemMapper.insert(evaluationItem);
        // 返回
        return evaluationItem.getId();
    }

    @Override
    public void updateEvaluationItem(StudyEvaluationItemUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateEvaluationItemExists(updateReqVO.getId());
        // 更新
        StudyEvaluationItemDO updateObj = StudyEvaluationItemConvert.INSTANCE.convert(updateReqVO);
        evaluationItemMapper.updateById(updateObj);
    }

    @Override
    public void deleteEvaluationItem(String id) {
        // 校验存在
        this.validateEvaluationItemExists(id);
        // 删除
        evaluationItemMapper.deleteById(id);
    }

    private void validateEvaluationItemExists(String id) {
        if (evaluationItemMapper.selectById(id) == null) {
            throw exception(EVALUATION_ITEM_NOT_EXISTS);
        }
    }

    @Override
    public StudyEvaluationItemDO getEvaluationItem(String id) {
        return evaluationItemMapper.selectById(id);
    }

    @Override
    public List<StudyEvaluationItemDO> getEvaluationItemList(Collection<String> ids) {
        return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : evaluationItemMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<StudyEvaluationItemDO> getEvaluationItemPage(StudyEvaluationItemPageReqVO pageReqVO) {
        return evaluationItemMapper.selectPage(pageReqVO);
    }

    @Override
    public List<StudyEvaluationItemDO> getEvaluationItemList(StudyEvaluationItemExportReqVO exportReqVO) {
        return evaluationItemMapper.selectList(exportReqVO);
    }



    /**
    * 获得评价项目列表, 根据 orgId 查询
    *
    * @param  orgId  机构编号 查询条件
    * @return 评价项目列表
    */
    @Override
    public List<StudyEvaluationItemDO> getEvaluationItemListByorgId(Integer orgId){
        return evaluationItemMapper.selectList(StudyEvaluationItemDO::getOrgId,orgId);
    }

}
